package edu.zut.main.thread;

public class ThreadSleep2 {

    public static void main(String[] args) {
        TicketThread ticketThread = new TicketThread();
        new Thread(ticketThread,"小花").start();
        new Thread(ticketThread,"小明").start();
        new Thread(ticketThread,"小李").start();

    }

}
//售票：多个窗口售票，模拟当窗口售票出现延迟时会出现的情况
//售票线程
class TicketThread implements Runnable{
    //余票
    private int ticketNum = 10;

    @Override
    public void run() {

        while (true){
            //当余票不足，结束
            if (ticketNum <= 0){
                break;
            }
            //如果售票过程中出现延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "售出了第" + ticketNum-- + "张票");

        }
    }
}

